const _ = require('../utils')
const { formItemBehavior } = require('../behaviors')

_.create('form-validator', {
  data: {
    children: [],
  },

  properties: {},

  relations: {
    formItem: {
      type: 'descendant',
      target: formItemBehavior,

      linked(target) {
        // eslint-disable-next-line no-console
        console.info('Form linked >', target.is)
      },
      linkChanged() {},
      unlinked() {},
    },
  },

  /**
   * Public functions
   */
  methods: {
    validate() {
      const elements = this.getRelationNodes('formItem')

      const errors = elements.map((e) => e.validate()).filter((e) => e === false)
      return errors.length > 0
    },
  },

  /**
   * Event handler functions
   */
})
